| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import { Head } from "fresh/runtime";
- import { createDefine, HttpError } from "fresh";
- import type { PageProps } from "fresh";
- import { checkToken } from "utils/server.ts";
- import { find } from "utils/db.ts";
- import TopBar from "../islands/TopBar.tsx";
- import Editor, { EditorMode } from "../islands/Editor.tsx";
- const define = createDefine<Record<never, never>>();
- interface PostProps {
- id: string;
- title: string;
- content: string;
- shared: boolean;
- isLogined: boolean;
- allowMode: EditorMode;
- }
- export const handler = define.handlers<PostProps>({
- GET(ctx) {
- const tokenUserId = checkToken(ctx.req);
- const postId = ctx.params.id;
- const post = find(
- "Post",
- tokenUserId
- ? { id: postId, user_id: tokenUserId }
- : { id: postId, shared: true },
- ["title", "content", "shared"],
- );
- if (post.length > 0) {
- return {
- data: {
- id: postId,
- isLogined: Boolean(tokenUserId),
- allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
- title: post[0][0] as string,
- content: post[0][1] as string,
- shared: post[0][2] as boolean,
- },
- };
- }
- throw new HttpError(404);
- },
- });
- export default function Post(props: PageProps<PostProps>) {
- return (
- <>
- <Head>
- <title>{props.data.title}</title>
- </Head>
- <div className="pd-page">
- <TopBar
- id={props.data.id}
- title={props.data.title}
- shared={props.data.shared}
- allowMode={props.data.allowMode}
- isLogined={props.data.isLogined}
- />
- <Editor
- id={props.data.id}
- content={props.data.content}
- allowMode={props.data.allowMode}
- />
- </div>
- </>
- );
- }
|